# -*- coding: UTF-8 -*- 
import random
import math
from threading import *

class Unite():
    def __init__(self, nom, id, pId, currenthp, maxHP, atk, defe, spd, x, y,sizex,sizey):
        self.id = id
        self.nom=nom
        self.iDjoueur = pId
        self.currenthp = currenthp
        self.maxHP = maxHP
        self.atk = atk
        self.defe = defe
        self.spd = spd
        self.shield = 0
        self.regHp = 0
        self.regShield = 0
        self.sizex=sizex
        self.sizey=sizey
        
        self.posx = x
        self.posy = y
        self.destx = x
        self.desty = y
        self.oldx = x
        self.oldy = y
        self.pasx = 0
        self.pasy = 0
        self.distance = 0
        
        self.target = 0
        self.size = 0
        
        self.isBio = False
        self.isMech = False
        self.canRepair = False
        self.canHeal = False
        self.canBuild = False
        self.researched = False
        self.ismoving = False
        self.isAttacking = False
        self.isAttacked = False
        self.cible = None
        self.isCombat = False
        
    def calcDistance2d(self,x1,y1,x2,y2):
        dx = (x2-x1)**2
        dy = (y2-y1)**2
        distance=math.sqrt(dx+dy)
        return distance
        
    def setDestination(self, x, y):
        """On redéfinie destx, desty. La distance est calculée"""
        self.destx = x
        self.desty = y
        self.distance = self.calcDistance2d(self.posx, self.posy, self.destx, self.desty)
                
        """Distance du parcours sur chaque axes"""
        dx = (self.destx - self.posx)
        dy = (self.desty - self.posy)
        
        """On redéfinie oldx et oldy pour la méthode compareDistances()"""
        self.oldx = self.posx
        self.oldy = self.posy
        
        pas = self.distance/self.spd
        
        self.pasx = dx / pas
        self.pasy = dy / pas
        self.ismoving = True
                
    def deplacerUnit (self): 
        """On avance"""

        self.posx = self.posx + self.pasx
        self.posy = self.posy + self.pasy
        self.compareDistances()
        
    
    def compareDistances(self):
        """La distance parcourru est comparé avec la distance à parcourir"""
        distParcouru = self.calcDistance2d(self.oldx,self.oldy,self.posx,self.posy)
        
        if distParcouru >= self.distance:
            self.arriver()
    
    def arriver (self):
        """L'unité est repositionné à sa destination s'il y arrive ou l'a dépasse"""
        self.posx = self.destx
        self.posy = self.desty
        self.distance = 0
        self.pasx = 0
        self.pasy = 0
        self.ismoving = False
        
    def arreterUnit (self):
        """Si l'unite est arrêté pendant son parcours"""
        self.destx = self.posx
        self.desty = self.posy
        self.distance = 0
        self.pasx = 0
        self.pasy = 0
        self.ismoving = False
        
    def attaquer(self,notreU,uniteEnnemi):
        if((abs(uniteEnnemi.posx-notreU.posx) <=50) and (abs(uniteEnnemi.posy-notreU.posy) <= 50)):
            notreU.isAttacking = True
            uniteEnnemi.currenthp -= notreU.atk
 
        else:
            if(not(uniteEnnemi.posx+49 > notreU.posx and uniteEnnemi.posx-49 < notreU.posx)):
                
                if(uniteEnnemi.posx+49 < notreU.posx):
                    
                    if(uniteEnnemi.posy+49 < notreU.posy):
                        notreU.setDestination(uniteEnnemi.posx+49,uniteEnnemi.posy+49)
                        
                    elif(uniteEnnemi.posy-49 > notreU.posy):
                        notreU.setDestination(uniteEnnemi.posx+49,uniteEnnemi.posy-49)
                        
                    else:
                         notreU.setDestination(uniteEnnemi.posx+49,notreU.posy)
                        
                elif(uniteEnnemi.posx-49 > notreU.posx):
                    
                    if(uniteEnnemi.posy+49 < notreU.posy):
                        notreU.setDestination(uniteEnnemi.posx-49,uniteEnnemi.posy+49)
                        
                    elif(uniteEnnemi.posy-49 > notreU.posy):
                        notreU.setDestination(uniteEnnemi.posx-49,uniteEnnemi.posy-49)
                    
                    else:
                        notreU.setDestination(uniteEnnemi.posx-49,notreU.posy)
                    
            else:
             
                if(uniteEnnemi.posy+49 < notreU.posy):
                    notreU.setDestination(notreU.posx,uniteEnnemi.posy+49)
                    
                elif(uniteEnnemi.posy-49 > notreU.posy):
                    notreU.setDestination(notreU.posx,uniteEnnemi.posy-49)
 
    

